<template>
  <div class="app-container">
    <div v-if="reader.type == 0">
      <iframe v-if="!reader.loading" :src="reader.src" frameborder="0" width="100%" :height="innerHeight + 'px'"
      ></iframe>
    </div>
    <div v-if="reader.type == 1">
      <el-row>
        <el-col span="3"/>
        <el-col span="15">
          <video controls autoplay :src="reader.src" :style="{height: innerHeight - 100 + 'px'}"/>
        </el-col>
      </el-row>
    </div>
    <div v-else style="text-align: center">加载中...</div>
  </div>
</template>

<script>
import { listTextbook, addTextBook, updateTextBook, read, info } from '@/api/exam/textbook'
import { isArray } from 'util'
import { getToken } from '@/utils/auth'

export default {
  name: 'Textbook',
  data() {
    let innerHeight = window.innerHeight - 200
    return {
      innerHeight: innerHeight,
      reader: {
        open: false,
        loading: false,
        // 弹出层标题（导入）
        title: '',
        src: '',
        type: 0   //0 文本 1视频
      }
    }
  },
  created() {
    this.reader.open = true
    this.reader.loading = true
    let id = this.urlParams()['id']
    info(id).then(response => {
      let info = response.data
      this.reader.type = info.type
      if (this.reader.type == 0) {
        read(id).then(resp => {
          let binaryData = []
          binaryData.push(resp)// res 后台返回的流数据
          let pdfUrl = ''
          pdfUrl = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }))
          this.reader.src = pdfUrl
          this.reader.loading = false
        })
      }else {
        this.reader.src = process.env.VUE_APP_BASE_API + '/textbook/read?id='+ id +'&Authorization=' + getToken();
      }
    })

  },
  methods: {
    setUrl() {
    },
    urlParams() {
      let geturl = window.location.href
      let getqyinfo = geturl.split('?')[1]
      if (!getqyinfo) {
        return {}
      }
      let getqys = getqyinfo.split('&')
      let obj = {}  //创建空对象，接收截取的参数
      for (let i = 0; i < getqys.length; i++) {
        let item = getqys[i].split('=')
        let key = decodeURIComponent(item[0])
        let value = decodeURIComponent(item[1])
        obj[key] = value
      }
      return obj
    }
  }
}
</script>
